<div class="card">
  <h4 class="card-header collapsed" data-toggle="collapse" data-target="#collapse-coupon">{{ heading_title }}</h4>
  <div id="collapse-coupon" class="collapse" data-parent="#accordion">
    <div class="card-body">
      <div class="row">
        <label for="input-coupon" class="col-md-2 col-form-label">{{ entry_coupon }}</label>
        <div class="col-md-10">
          <div class="input-group">
            <input type="text" name="coupon" value="{{ coupon }}" placeholder="{{ entry_coupon }}" id="input-coupon" class="form-control"/>
            <div class="input-group-append">
              {% if not coupon %}
                <button type="button" id="button-coupon" data-loading-text="{{ text_loading }}" class="btn btn-primary">{{ button_coupon }}</button>
              {% else %}
                <button type="button" id="button-remove" data-loading-text="{{ text_loading }}" class="btn btn-danger">{{ button_remove }}</button>
              {% endif %}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script type="text/javascript"><!--
  $('#button-coupon').on('click', function() {
      $.ajax({
          url: 'index.php?route=extension/opencart/total/coupon|save',
          type: 'post',
          data: 'coupon=' + encodeURIComponent($('input[name=\'coupon\']').val()),
          dataType: 'json',
          beforeSend: function() {
              $('#button-coupon').button('loading');
          },
          complete: function() {
              $('#button-coupon').button('reset');
          },
          success: function(json) {
              $('.alert-dismissible').remove();

              if (json['error']) {
                  $('#collapse-coupon .card-body').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
              }

              if (json['redirect']) {
                  location = json['redirect'];
              }
          },
          error: function(xhr, ajaxOptions, thrownError) {
              alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
      });
  });

  $('#button-remove').on('click', function() {
      $.ajax({
          url: 'index.php?route=extension/opencart/total/coupon|remove',
          dataType: 'json',
          beforeSend: function() {
              $('#button-coupon').button('loading');
          },
          complete: function() {
              $('#button-coupon').button('reset');
          },
          success: function(json) {
              $('.alert-dismissible').remove();

              if (json['redirect']) {
                  location = json['redirect'];
              }
          },
          error: function(xhr, ajaxOptions, thrownError) {
              alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
      });
  });
  //--></script>
</div>

